package com.seele.tigerwallet.ui.activtiy;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.seele.tigerwallet.R;
import com.seele.tigerwallet.model.AssetsModel;
import com.seele.tigerwallet.model.UserInfo;
import com.seele.tigerwallet.ui.application.SeeleApplication;
import com.seele.tigerwallet.ui.base.ToolbarBaseActivity;
import com.seele.tigerwallet.ui.utils.ShareUtil;
import com.seele.tigerwallet.ui.utils.StatusBarUtils;
import com.seele.tigerwallet.utils.QRCodeHelper;

public class ReceiptActivity extends ToolbarBaseActivity implements View.OnClickListener {

    private ImageView mIvQRCode;
    private TextView mTvAddress;
    private ClipboardManager mClipboardManager;
    private AssetsModel mAssetsModel;
    private TextView mTitleTv;

    @Override
    protected int getContainerViewId() {
        return R.layout.activity_receipt;
    }

    @Override
    protected void initView() {
        StatusBarUtils.with(this).init();
        Intent intent = getIntent();
        mIvQRCode = $f(R.id.ivQR);
        mTvAddress = $f(R.id.tv_address);
        mRightImg.setBackground(getResources().getDrawable(R.drawable.receip_ripple_bg));
        mImgBack.setBackground(getResources().getDrawable(R.drawable.receip_ripple_bg));
        mTitleTv=$f(R.id.title_tv);
//        mTvAddress.setBackground(getBackgroundByAddress());
        Bundle bundle = (Bundle) intent.getExtras();
        mAssetsModel = (AssetsModel) bundle.getSerializable(ReceiptActivity.class.getSimpleName());
        setLeftImg(R.drawable.ic_back);
        setRightImg(R.drawable.ic_share);
        tool_bar.setBackgroundColor(getResources().getColor(R.color.color_3A3D5C));

        Typeface font = Typeface.createFromAsset(SeeleApplication.getInstance().getAssets(), "fonts/CENTURY_GOTHIC_BOLD.TTF");
//        mTvAddress.setTypeface(font);
        String message = "{\"Custom\":\"%s\",\"CoinAddress\":\"%s\",\"WalletAddress\":\"%s\",\"CoinName\":\"%s\",\"CoinSymbolID\":\"%s\"}";
        message = String.format(message, "", mAssetsModel.addr, UserInfo.get().address, mAssetsModel.symbol, mAssetsModel.id);
        mTvAddress.setText(UserInfo.get().address);
        Bitmap qrBitmap = QRCodeHelper.generateBitmap(message, 200, 200);
        mIvQRCode.setImageBitmap(qrBitmap);
        mClipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);

        if (null != mAssetsModel) {
            if (null != mAssetsModel) {
                if (!TextUtils.isEmpty(mAssetsModel.symbol)) {
                   setTitle(mAssetsModel.symbol);
                    setTextColor(getResources().getColor(R.color.color_FFFFFF));

                    String title = String.format((String) getResources().getText(R.string.receipt_title), mAssetsModel.symbol);
                    mTitleTv.setText(title);
                }

            }
        }

    }


    @Override
    protected void onRightImg() {
        onRightClick();
    }

    public void onRightClick() {
        try {
            copyAddress();
            CharSequence charSequence = mClipboardManager.getPrimaryClip().getItemAt(0).coerceToText(ReceiptActivity.this);
            String content = charSequence.toString();
            if (!TextUtils.isEmpty(content)) {
                ShareUtil.shareText(ReceiptActivity.this, content);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
//

    public void onBtnClick(View v) {
        if (v.getId() == R.id.btn_copy) {
            copyAddress();
            Toast.makeText(ReceiptActivity.this, R.string.copy_toast, Toast.LENGTH_LONG).show();


        }
    }

    private void copyAddress() {
        ClipData mClipData = ClipData.newPlainText("Label", UserInfo.get().address);
        mClipboardManager.setPrimaryClip(mClipData);
    }






    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.right_img:
                onRightClick();
                break;

        }

    }

    /**
     * 创建一个圆角背景
     *
     * @return
     */
    private GradientDrawable getBackgroundByAddress() {
        GradientDrawable drawable = new GradientDrawable();
        drawable.setColor(ContextCompat.getColor(this, R.color.color_F4F4F4));
        DisplayMetrics metrics = getResources().getDisplayMetrics();
        float radius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, metrics);
        drawable.setCornerRadius(radius);
        return drawable;
    }
}
